1 // -----------------------------------------------------------------------------------------------
2 // ------------------------------- UART serial data transfer -------------------------------------
3 // -----------------------------------------------------------------------------------------------
5 extern void read433_handler( void );
7 void initserial(unsigned long baud);
14 static volatile uint8_t srx_done, stx_count;
15 static volatile uint8_t srx_data, srx_mask, srx_tmp, stx_data;
17 #define SRX_STR_MAX 30
19 // char srx_str[SRX_STR_MAX];
21 uint8_t srx_ready = false;
30 char val[SRX_STR_MAX-7];
33 struct SHORT_COM_STRUCT {
36 char val[SRX_STR_MAX-5];
40 char str [SRX_STR_MAX];
41 struct COM_STRUCT cmd;
42 struct SHORT_COM_STRUCT short_cmd;
52 // ------------------------------- serial strings and symbols transfer ---------------------------
71 char getche(void) // echo
79 while (*s) putch(*s++);
83 void putstr_P(PGM_P str){
86 while ( pgm_read_byte(s) ) putch( pgm_read_byte(s++) );
90 // ------------------------------------ low level UART --------------------------------------
92 static uint16_t bit_time, start_bit_time;
94 void initSerial(unsigned long baud)
97 set_output( DDRA, TX );
98 output_high( PORTA, TX ); // TX
99 output_high( PORTA, RX ); // RX
101 bit_time = (F_CPU / 8) / baud ;
102 start_bit_time = bit_time + (bit_time >> 1);
104 // OCR1A top value A for Counter1
105 // TCNT1 8-bit register contains the value of Timer/Counter1
107 OCR1A = TCNT1 + 1; // probably low bit of 16 bit timer value on a84
109 // TCCR1 Timer/Counter1 Control Register
110 // CS10 prescaler none
111 // CS12 prescaler CK/8
112 // CS10 + CS12 prescaler CK/16 (in PLL mode)
113 // COM1A1 + COM1A0 Set the OC1A output line on compare match
115 // TCCR1 |= (1 << CS10) | (1 << CS12);
116 // TCCR1 |= (1 << COM1A1) | (1 << COM1A0);
118 TCCR1B |= (1 << CS11);
119 TCCR1A |= (1 << COM1A1) | (1 << COM1A0); // PA6? output TX
121 // OCIE1A Timer/Counter1 Output Compare Match A Interrupt Enable
123 // TIMSK |= (1 << OCIE1A);
124 TIMSK1 |= (1 << OCIE1A);
126 // PCMSK Pin Change Enable Mask 0 (for RX)
127 // PCIE Pin Change Interrupt Enable (for RX)
129 // PCMSK |= (1 << PCINT0);
130 // GIMSK |= (1 << PCIE);
132 PCMSK0 |= (1 << PCINT7);
133 GIMSK |= (1 << PCIE0); // PCINT7..0 pin
139 // ISR(PCINT0_vect) // RX pin was pinged
140 ISR(PCINT0_vect) // RX pin was pinged Pin change interrupt 0 (source 7, see above)
143 if ( PINA & (1 << RX) ) return;
145 // OCR1B top value B for Counter1
146 // TCNT1 8-bit register contains the value of Timer/Counter1
148 OCR1B = TCNT1 + (uint16_t) (start_bit_time); // maybe OCR1BL
153 // TIFR Timer/Counter Interrupt Flag Register
154 // OCF1B bit is set (one) when compare match occurs in OCR1B - Output Compare Register 1A
155 // forsed interrupt execution ???
156 // TIFR = (1 << OCF1B);
158 // TIFR |= (1 << OCF1B);
159 TIFR1 |= (1 << OCF1B);
161 ////// TIFR &= ~(1 << OCF1A); // added
163 if ( !(PINA & (1 << RX)) ) { // when RX not changed ???
165 // Enable compare Timer/Counter1 Output Compare Match A & Match B Interrupt
166 /////// TIMSK = (1 << OCIE1A) ^ (1 << OCIE1B);
168 // TIMSK |= (1 << OCIE1A) | (1 << OCIE1B);
169 TIMSK1 |= (1 << OCIE1A) | (1 << OCIE1B);
171 /////// TIMSK &= ~(1 << TOIE1); // added
173 /////// TIMSK |= (1 << OCIE1A);
174 /////// TIMSK |= (1 << OCIE1B);
176 // Pin Change disable Mask for PB0
177 // PCMSK &= ~(1 << PCINT0);
178 PCMSK0 &= ~(1 << PCINT7);
184 ISR(TIM1_COMPB_vect) // RX
190 if (in & (1 << RX)) srx_tmp |= srx_mask;
192 // OCR1B top value B for Counter1
193 OCR1B = OCR1B + bit_time;
198 // collect string until '\n' (0x0a), for uninterruptable receiveing of strings
199 // if ( srx_cnt < SRX_STR_MAX ) srx_str[ srx_cnt++ ] = srx_data;
200 if ( srx_cnt < SRX_STR_MAX ) im.str[ srx_cnt++ ] = srx_data;
201 if ( srx_data == 0x0a ) {
202 // srx_str[ srx_cnt ] = 0;
203 im.str[ srx_cnt ] = 0;
208 // Enable compare Timer/Counter1 Output Compare Match A Interrupt
209 // TIMSK |= (1 << OCIE1A);
210 // TIMSK &= ~(1 << OCIE1B);
212 TIMSK1 |= (1 << OCIE1A);
213 TIMSK1 &= ~(1 << OCIE1B);
215 // Pin Change Enable Mask for PB0
216 // PCMSK |= (1 << PCINT0);
217 PCMSK0 |= (1 << PCINT7);
223 ISR(TIM1_COMPA_vect) // TX
228 // PORTB ^= 0b100; // check sampling frequency
230 // OCR1A top value A for Counter1
231 OCR1A = OCR1A + bit_time;
240 if (!(stx_data & 1)) {
241 // Set OC1A output line (COM1A1 initially 1)
242 // TCCR1 |= (1 << COM1A0);
243 TCCR1A |= (1 << COM1A0);
245 // Clear OC1A output line (COM1A1 initially 1)
246 // TCCR1 &= ~(1 << COM1A0);
247 TCCR1A &= ~(1 << COM1A0);
253 // Clear OC1A output line (COM1A1 initially 1)
254 // TCCR1 &= ~(1 << COM1A0);
255 TCCR1A &= ~(1 << COM1A0);